"""
@Author  : 康帅
@Time    : 2021/3/12 16:08
@Function: 中介模式案列：设备管理器
"""

from enum import Enum

from DesignMode.AgentMode.DeviceMgrBase import DeviceMgr

"""
中介模式应用场景：
1.一组对象以定义良好但复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解
2.一个对象引用其他很多对象并且直接与这些对象通信，导致难以复用该对象
3.想通过一个中间类来封装多个类中的行为，同时又不想生成太多的子类
"""


class DeviceType(Enum):
    """ 设备类型 """
    TypeSpeaker = 1
    TypeMicrophone = 2
    TypeCamera = 3


class DeviceItem:
    """ 设备项 """

    def __init__(self, id, name, type, is_default=False):
        self.__id = id
        self.__name = name
        self.__type = type
        self.__is_default = is_default

    def __str__(self):
        return "type:" + str(self.__type), "id:" + str(self.__id), "name:" + str(self.__name), "is_default:" + str(
            self.__is_default)

    def get_id(self):
        return self.__id

    def get_name(self):
        return self.__name

    def get_type(self):
        return self.__type

    def is_default(self):
        return self.__is_default


class DeviceList:
    """ 设备列表 """

    def __init__(self):
        self.__devices = []

    def add_device(self, device_item):
        self.__devices.append(device_item)

    def get_count(self):
        return len(self.__devices)

    def get_by_idx(self, idx):
        if idx < 0 or idx >= self.get_count():
            return None
        return self.__devices[idx]

    def get_by_id(self, id):
        for item in self.__devices:
            if item.get_id() == id:
                return item
        return None


class SpeakerMgr(DeviceMgr):
    """ 扬声器类 """

    def __init__(self):
        self.__cur_device_id = None

    def enumerate(self):
        """ 枚举设备列表 """
        devices = DeviceList()
        devices.add_device(
            DeviceItem("abc-ddd-366879-aaa", "Realtek High Defintion Audio", DeviceType.TypeSpeaker))
        devices.add_device(
            DeviceItem("def-aaa-888888-cba", "NVIDIA High Defintion Audio", DeviceType.TypeSpeaker, True))
        return devices

    def active(self, device_id):
        """ 激活指定设备作为当前要用的设备 """
        self.__cur_device_id = device_id

    def get_cur_device_id(self):
        return self.__cur_device_id


class DeviceUtil:
    """ 设备工具类 """

    def __init__(self):
        self.__mgrs = {}
        self.__mgrs[DeviceType.TypeSpeaker] = SpeakerMgr()
        # self.__micronpone = MicronponeMgr()
        # self.__camera = CameraMgr()

    def __get_device_mgr(self, type):
        return self.__mgrs[type]

    def get_device_list(self, type):
        return self.__get_device_mgr(type).enumerate()

    def active(self, type, device_id):
        self.__get_device_mgr(type).active(device_id)

    def get_cur_device_id(self, type):
        return self.__get_device_mgr(type).get_cur_device_id()


if __name__ == '__main__':
    device_util = DeviceUtil()
    device_list = device_util.get_device_list(DeviceType.TypeSpeaker)
    print("麦克风设备列表：")
    if device_list.get_count() > 0:
        # 设置第一个为要用的设备
        device_util.active(DeviceType.TypeSpeaker, device_list.get_by_idx(0).get_id())
    for idx in range(0, device_list.get_count()):
        device = device_list.get_by_idx(idx)
        print({"id": device.get_id(), "name": device.get_name(),
               "type": device.get_type(), "is_default": device.is_default()})
    print("当前使用的设备：" + device_list.get_by_id(device_util.get_cur_device_id(DeviceType.TypeSpeaker)).get_name())
